$(function(){
	layui.use(['form','util'],function(){ 
		var form = layui.form
		,layer = layui.layer
		,util = layui.util;
		
		$.ajax({
			 type : 'get'
			 ,dataType : 'json'
			 ,url : access_url+'/cache/loadcachedata/getdictsbypcode?parent_code=2000'
			 ,success : function(data){
				console.log("data",data);
				if(data.code != 200){
					layer.msg(data.msg, {icon: 5});
					return;
				}
				
				var dictlist = data.retlist;
				
				var dictLen = dictlist.length;
				
				var orgTypeHtml = '';
				
				for (var i = 0; i < dictLen; i++) {
					var dict = dictlist[i];
					var htmlModel = '<option value="{word_value}">{word_mean}</option>'
					var opHtml = htmlModel.format(dict);
					orgTypeHtml += opHtml;
				}
				
				$('#org_type').append(orgTypeHtml);
				
				form.val('org-form');
		 	 }
		})
		
		util.event('lay-demo',{
			saveOrg : function(othis){
				var formData = form.val('org-form');
				
				var orgid = formData.orgid;
				var orgname = formData.orgname;
				var parent_id = $('#parent_id').attr('data-value');
				var org_type = formData.org_type;
				var remark = formData.remark;
				
				layer.load(2);
				
				$.ajax({
					 type : 'post'
					 ,dataType : 'json'
					 ,url : access_url+'/system/systemmanage/addorg'
					 ,contentType : 'application/json;charset=UTF-8'
					 ,data : JSON.stringify({
						 orgid : orgid
						 ,orgname : orgname
						 ,parent_id : parent_id
						 ,org_type : org_type
						 ,remark : remark
					 })
					 ,success : function(data){
						layer.closeAll('loading');
						if(data.code != 200){
							layer.msg(data.msg, {icon: 5});
							return;
						}
						parent.layer.alert(data.msg,{icon:6});
						parent.loadTable();
						parent.layer.closeAll('iframe');
					 }
				 	 ,error : function(){
				 		layer.closeAll('loading');
				 		layer.msg('保存失败,请联系管理员', {icon: 5});
				 	 }
				})
			}
		})
	})
})


$('#parent_id').click(function(){
	layer.open({
		type : 2
		,title : '选择组织机构'
		,area : ['300px','450px']
		,offset : '20px'
		,fixed : false
		,content : 'chooseOrg.html' 
		,resize : false
		,success: function (layero, index) {
            // 获取子页面的iframe
            var iframe = window['layui-layer-iframe' + index];
            // 向子页面的全局函数child传参
            iframe.transmitParam(true);
        }
	})
})

function transmitParam(param){
	var pOrgid = param.orgid;
	var pOrgname = param.orgname;
	$('#parent_id').val(pOrgname);
	$('#parent_id').attr('data-value',pOrgid);
}